//
//  PlayerHelpers.m
//  YinYueBoFang_Forever
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "PlayerHelpers.h"
#import <AVFoundation/AVFoundation.h>
@interface PlayerHelpers()
@property(nonatomic,strong)AVPlayer *player;

@property(nonatomic,assign)BOOL isPlaying;

@property(nonatomic,strong)NSTimer *timer;


@end



@implementation PlayerHelpers

+(PlayerHelpers *)defaultPlayers
{
    static dispatch_once_t onceToken;
    static PlayerHelpers *playerHelps = nil;
    dispatch_once(&onceToken, ^{
        playerHelps = [[PlayerHelpers alloc] init];
        playerHelps.player = [[AVPlayer alloc] init];
        playerHelps.playStatus  = 0;
        [[NSNotificationCenter defaultCenter] addObserver:playerHelps selector:@selector(musicDidFinish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
        
    });
    return playerHelps;
}
-(void)musicDidFinish
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(currentMusicDidFinish)]) {
        [self.delegate currentMusicDidFinish];
    }
}



-(void)playWithURL:(NSString *)urlString
{
    if (self.player.currentItem) {
        [self.player.currentItem removeObserver:self forKeyPath:@"status"];
    }
    
    
    NSURL *url = [NSURL URLWithString:urlString];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
    
    //观察者模式
    [item addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew) context:nil];
    [self.player replaceCurrentItemWithPlayerItem:item];
    
}

-(void)play
{
    [self.player play];
    self.isPlaying = YES;
    
    if (self.timer == nil) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
        
    }
}


-(void)timeAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(musicPlayTime:)]) {
        float progress = self.player.currentTime.value/self.player.currentTime.timescale;
        [self.delegate musicPlayTime:progress];
    }
}

-(void)pause
{
    [self.player pause];
    self.isPlaying = NO;
    [self.timer invalidate];
    self.timer = nil;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"status"]) {
        switch ([change[@"new"]integerValue]) {
            case AVPlayerItemStatusUnknown:
                break;
                case AVPlayerItemStatusReadyToPlay:
                [self play];
                break;
                case AVPlayerItemStatusFailed:
                break;
            default:
                break;
        }
    }
}

-(void)stop
{
    [self pause];
}

-(void)seekToTime:(float)time
{
    CMTime musicTime = CMTimeMakeWithSeconds(time, self.player.currentTime.timescale);
    [self pause];
    [self.player seekToTime:musicTime completionHandler:^(BOOL finished) {
        if (finished) {
            [self play];
        }
    }];
}

-(BOOL)isCurrentMusicWithUrlString:(NSString *)urlString
{
    //获取当前播放音乐的URL
    NSString *string = [(AVURLAsset *)self.player.currentItem.asset URL].absoluteString;
    
    return [urlString isEqualToString:string];
}


@end
